home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat50 / cprg / cliumain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-09  |  3.3 KB  |  147 lines

  1. /* CliUMain.C : modification de UMain.C fonction _main()        */
  2. /* Utilisation exclusive des programmes linkés avec CliUMain.O  */
  3. /* sous CLI, pas de fonctionnement avec le WorkBench            */
  4. /* Remplace "_main.c"                                           */
  5.  
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #include <ios1.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <proto/dos.h>
  14. #include <proto/exec.h>
  15.  
  16. #define MAXARG 32              /* maximum command line arguments */
  17. #define QUOTE  '"'
  18.  
  19. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  20.  
  21. #ifndef TINY
  22. extern int _fmode,_iomode;
  23. extern int (*_ONBREAK)();
  24. extern int CXBRK();
  25. #endif
  26.  
  27. extern struct UFB _ufbs[];
  28. int argc;                       /* arg count */
  29. char **targv, *argv[MAXARG];     /* arg pointers */
  30.  
  31. /**
  32. *
  33. * name         _main - process command line, open files, and call "main"
  34. *
  35. * synopsis     _main(line);
  36. *              char *line;     ptr to command line that caused execution
  37. *
  38. * description   This function performs the standard pre-processing for
  39. *               the main module of a C program.  It accepts a command
  40. *               line of the form
  41. *
  42. *                       pgmname arg1 arg2 ...
  43. *
  44. *               and builds a list of pointers to each argument.  The first
  45. *               pointer is to the program name.  For some environments, the
  46. *               standard I/O files are also opened, using file names that
  47. *               were set up by the OS interface module XCMAIN.
  48. *
  49. **/
  50.  
  51. void _main(line)
  52. register char *line;
  53. {
  54.  register char **pargv;
  55.  register int x;
  56.  
  57.  
  58. /*
  59.  *
  60.  * Build argument pointer list
  61.  *
  62.  */
  63.  while (argc < MAXARG)
  64.        {
  65.         while(isspace(*line)) line++;
  66.  
  67.         if(*line == '\0')     break;
  68.  
  69.         pargv = &argv[argc++];
  70.  
  71.         if(*line == QUOTE)
  72.             {
  73.              *pargv = ++line;  /* ptr inside quoted string */
  74.  
  75.              while ((*line) && (*line != QUOTE)) line++;
  76.  
  77.              if (*line == '\0') _exit(1);
  78.              else               *line++ = 0;  /* terminate arg */
  79.  
  80.             }
  81.         else{           /* non-quoted arg */
  82.              *pargv = line;
  83.  
  84.              while ((*line) && (!isspace(*line))) line++;
  85.  
  86.              if (*line == 0) break;
  87.              else            *line++ = 0;  /* terminate arg */
  88.             }
  89.         }  /* while */
  90.  
  91.  targv = (char **)&argv[0];
  92.  
  93. /*
  94.  *
  95.  * Open standard files
  96.  *
  97.  */
  98. #ifndef TINY
  99.  
  100.  if(argc == 0) exit(0); /* running under workbench      */
  101.  else{                  /* running under CLI            */
  102.       _ufbs[0].ufbfh = Input();
  103.       _ufbs[1].ufbfh = Output();
  104.       _ufbs[2].ufbfh = Output(); /* Open("*", MODE_OLDFILE); */
  105.       x = UFB_NC;                /* do not close CLI defaults    */
  106.      }
  107.  
  108.  _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  109.  _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  110. /*
  111.  _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  112. */
  113.  _ufbs[2].ufbflg = _ufbs[0].ufbflg;
  114.  
  115.  x = (_fmode) ? 0 : _IOXLAT;
  116.  stdin->_file = 0;
  117.  stdin->_flag = _IOREAD | x;
  118.  stdout->_file = 1;
  119.  stdout->_flag = _IOWRT | x;
  120. /*
  121.  stderr->_file = 2;
  122.  stderr->_flag = _IORW | x;
  123. */
  124.  stderr->_file = stdin->_file;
  125.  stderr->_flag = stdin->_flag;
  126.  
  127. /*      establish control-c handler */
  128.  
  129.  _ONBREAK = CXBRK;
  130.  
  131. #endif
  132.  
  133. /*
  134.  *
  135.  * Call user's main program
  136.  *
  137.  */
  138.  
  139.  main(argc,targv);              /* call main function */
  140.  
  141.  #ifndef TINY
  142.  exit(0);
  143.  #else
  144.  _exit(0);
  145.  #endif
  146. }
  147.